Given a string s
, return the number of homogenous substrings ofs
. Since the answer may be too large, return it modulo109 + 7
.
A string is homogenous if all the characters of the string are the same.
A substring is a contiguous sequence of characters within a string.
Input: s = "abbcccaa" Output: 13 Explanation: The homogenous substrings are listed as below: "a" appears 3 times. "aa" appears 1 time. "b" appears 2 times. "bb" appears 1 time. "c" appears 3 times. "cc" appears 2 times. "ccc" appears 1 time. 3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.
Input: s = "xy" Output: 2 Explanation: The homogenous substrings are "x" and "y".
Input: s = "zzzzz" Output: 15
1 <= s.length <= 105
s
consists of lowercase letters.
implSolution{pubfncount_homogenous(s:String) -> i32{let s = s.as_bytes();letmut count = 1;letmut ret = 1;for i in1..s.len(){if s[i] != s[i - 1]{ count = 0;} count += 1; ret = (ret + count) % 1_000_000_007;} ret }}